using System;
using System.Data;
using 第二次作业.entity;
using 第二次作业.entity.impl;

namespace 第二次作业
{
    public partial class Form1 : Form
    {
        private ATM atm = new ATM();

        public Form1()
        {
            InitializeComponent();

            atm.BigMoneyFetched += Atm_BigMoneyFetched;
        }

        private void addAccountBttn_Click(object sender, EventArgs e)
        {
            string user;
            if (string.IsNullOrEmpty(nameBox.Text))
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            else
            {
                user = nameBox.Text;
            }

            try
            {
                CreditAccount account = new CreditAccount(user, "123456");
                atm.bank.addAccount(account);
                MessageBox.Show("添加成功！");
            }
            catch (DuplicateNameException exception)
            {
                MessageBox.Show(exception.Message);
            }

            nameBox.Clear();
        }

        private void depositBttn_Click(object sender, EventArgs e)
        {
            string user;
            string money;

            if (string.IsNullOrEmpty(nameBox.Text))
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            else
            {
                user = nameBox.Text;
            }

            if (string.IsNullOrEmpty(moneyBox.Text))
            {
                MessageBox.Show("存款金额不能为空！");
                return;
            }
            else
            {
                money = moneyBox.Text;
            }

            atm.depositMoney(user, double.Parse(money));

            double balance = atm.bank[user].balance;
            MessageBox.Show("存钱成功！余额为"+balance.ToString());

            nameBox.Clear() ;
            moneyBox.Clear() ;
        }

        private void withdrawBttn_Click(object sender, EventArgs e)
        {
            string user;
            string money;

            if (string.IsNullOrEmpty(nameBox.Text))
            {
                MessageBox.Show("用户名不能为空！");
                return;
            }
            else
            {
                user = nameBox.Text;
            }

            if (string.IsNullOrEmpty(moneyBox.Text))
            {
                MessageBox.Show("存款金额不能为空！");
                return;
            }
            else
            {
                money = moneyBox.Text;
            }

            try
            {
                atm.withdrawMoney(user, double.Parse(money));
            }
            catch (InvalidOperationException exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }
            catch (BadCashException exception)
            {
                MessageBox.Show(exception.Message);
                return;
            }
            double balance = atm.bank[user].balance;
            MessageBox.Show("取款成功!余额为" + balance.ToString());

            nameBox.Clear() ;
            moneyBox.Clear() ;
        }

        private void Atm_BigMoneyFetched(object sender, BigMoneyArgs e)
        {
            MessageBox.Show($"警告：用户 {e.accountName} 取走了大笔金额 {e.amount} 元！");
        }
    }
}